#include <cstdio>
#include <deque>
#include <queue>
#include <cstring>
using namespace std;

int pnum;
int t1, t2, t3, t4, t5;
int tq;

int pdata[10][30];
int opcount[10];

deque<int> qwait;
queue<int> qblock;
int poffset[10];
int varible[30];
int lock_id;

void get_prog(int id) {
	char tmpbuf[20];
	char buf[20];
	int k = 0;
	while (true) {
		fgets(tmpbuf, 20, stdin);
		int tmpbuf_len = strlen(tmpbuf);
		tmpbuf[--tmpbuf_len] = '\0';
		int tmp = 0;
		bool flag = false;
		for (int i=0; i<tmpbuf_len; i++) {
			if (flag || tmpbuf[i] != ' ') {
				buf[tmp++] = tmpbuf[i];
				if (!flag) flag = true;
			}
		}
		for (int i=tmp-1; i>=0; i--) {
			if (buf[i] != ' ') {
				buf[i+1] = '\0';
				break;
			}
		}
		if (strcmp(buf, "lock") == 0) {
			pdata[id][k++] = 3;
		} else if (strcmp(buf, "unlock") == 0) {
			pdata[id][k++] = 4;
		} else if (strcmp(buf, "end") == 0) {
			pdata[id][k++] = 5;
			break;
		} else {
			char buf2[20] = { 0 };
			for (int i=0; i<5; i++)
				buf2[i] = buf[i];
			if (strcmp(buf2, "print") == 0) {
				char var = buf[6];
				pdata[id][k++] = 10 + var-'a'+1;
			} else if (strchr(buf, '=') != NULL) {
				int l = 0;
				for (int i=0; i<tmp; i++) {
					if (buf[i] != ' ')
						buf2[l++] = buf[i];
				}
				buf2[l] = '\0';
				char var;
				int val;
				sscanf(buf2, "%c=%d", &var, &val);
				int opcode = (var-'a'+1) * 1000 + val;
				pdata[id][k++] = opcode;
			}
		}
	}
	opcount[id] = k;
}

int print_debug() {
	printf("Debug start...\n");
	for (int i=0; i<pnum; i++) {
		for (int j=0; j<opcount[i]; j++) {
			printf("%d\n", pdata[i][j]);
		}
		printf("\n");
	}
}

int get_optime(int op) {
	if (op == 3) {
		return t3;
	} else if (op == 4) {
		return t4;
	} else if (op == 5) {
		return t5;
	} else if (op >= 1000) {
		return t1;
	} else {
		return t2;
	}
}

void solve() {
	memset(poffset, 0, sizeof(poffset));
	memset(varible, 0, sizeof(varible));
	lock_id = -1;
	for (int i=0; i<pnum; i++) {
		qwait.push_back(i);
	}
	while (!qwait.empty()) {
		int id = qwait.front(); qwait.pop_front();
		int time = tq;
		int i;
		bool block = false;
		for (i=poffset[id]; i<opcount[id]; i++) {
			if (time <= 0) break;
			int op = pdata[id][i];
			int curr_t = get_optime(op);
			time -= curr_t;
			if (op == 3) {
				if (lock_id == -1) {
					lock_id = id;
				} else {
					qblock.push(id);
					time = 0;
					block = true;
					break;
				}
			} else if (op == 4) {
				lock_id = -1;
				if (!qblock.empty()) {
					int temp_id = qblock.front(); qblock.pop();
					qwait.push_front(temp_id);
				}
			} else if (op == 5) {
				break;
			} else if (op >= 1000) {
				int var = op / 1000;
				int val = op % 1000;
				varible[var] = val;
			} else {
				int var = op - 10;
				printf("%d: %d\n", id+1, varible[var]);
			}
		}
		poffset[id] = i;
		if (i < opcount[id]-1 && !block) {
			qwait.push_back(id);
		}
	}
}

int main() {
	int kase;
	scanf("%d", &kase);
	bool flag = false;
	while (kase--) {
		if (flag) printf("\n");
		else flag = true;
		scanf("%d%d%d%d%d%d%d\n", &pnum, &t1, &t2, &t3, &t4, &t5, &tq);
		memset(pdata, 0, sizeof(pdata));
		for (int i=0; i<pnum; i++) {
			get_prog(i);
		}
		//print_debug();
		//printf("\n\n\n");
		solve();
		//printf("\n");
	}
	return 0;
}
